//## 휴일근무  정보 저장 ####################
function fnSave()
{
	if (!$("#userId").val())
	{
		alert("근무자를 바르게 선택해 주세요!");
		$("#userId").focus();
		return;
	}
	if (!$("#workDt").val())
	{
		alert("근무일자를 바르게 입력해 주세요!");
		$("#workDt").focus();
		return;
	}
	if (!$("#workTm").val())
	{
		alert("근무 시간을 바르게 입력해 주세요!");
		$("#workTm").focus();
		return;
	}
	var valUrl  = '/action/offDayWork/save.asp';
	var param  = "userId="+$("#userId").val();
		param += "&workDt="+$("#workDt").val();
		param += "&workTm="+$("#workTm").val();
		param += "&seq="+$("#seq").val();
		param += "&flag="+$("#flag").val();
	
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 휴일근무  정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	clstTable("Grid01");
	$("#tr01Result").css("display","");
	setNullRow("loadSpan","1");
	var valUrl = "/action/offDayWork/list.asp";
	var param = "findStartDt="+$("#findStartDt").val();
		param += "&findEndDt="+$("#findEndDt").val();
		param += "&findUserId="+$("#findUserId").val();

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
			fnReset();
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	clstTable("Grid01");

	$("#tr01Result").css("display","none");
	$("#loadSpan1").html("");
	var nodeCnt = $(G_grid01Rslt).find("OffWork").find("OffDayWork").length;

	if(nodeCnt == 0)
	{
		$("#tr01Result").css("display","");
		$("#loadSpan1").html("검색된 자료가 없습니다.");
		$("#grid01Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	var ttlTm = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var userNm		= $(G_grid01Rslt).find("OffWork").find("OffDayWork").eq(j).find("userNm").text();
		var workDt		= $(G_grid01Rslt).find("OffWork").find("OffDayWork").eq(j).find("workDt").text();
		var workTm		= $(G_grid01Rslt).find("OffWork").find("OffDayWork").eq(j).find("workTm").text();;
		var seq				= $(G_grid01Rslt).find("OffWork").find("OffDayWork").eq(j).find("seq").text();
		var flag				= $(G_grid01Rslt).find("OffWork").find("OffDayWork").eq(j).find("flag").text();

		ji = j+1;
		
		var showFlag = "";
		var tagFirst = "";
		var tagLast = "";
		if(flag == "Y") showFlag = "유효";
		else showFlag = "삭제";
		
		if (flag == "N") { var bgColor = "#F2F2F2"; tagFirst = "<font color=\"#CCCCCC\">"; tagLast = "</font>"; }
		else { 
			var bgColor = "#FFFFFF"; 
			if (workTm < 80) ttlTm = ttlTm + parseInt(workTm,10); 
			else 
			{
				if (workTm == "88") workTm = "휴가";
				else workTm = "정상";
			}
		}

		grid01Array[0] = ji;
		grid01Array[1] = tagFirst + workDt + tagLast;
		grid01Array[2] = tagFirst + workTm + tagLast;
		grid01Array[3] = tagFirst + userNm + tagLast;
		grid01Array[4] = showFlag;
		grid01Array[5] = "&nbsp;";

		

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid01", grid01Array, bgColor, ",,,,,,", "fnSetOffWork('"+seq+"')", boldLine);
	}
	
		grid01Array[0] = "&nbsp;";
		grid01Array[1] = "<b>TOTAL</b>";
		grid01Array[2] = "<b>"+ttlTm+"</b>";
		grid01Array[3] = "&nbsp;";
		grid01Array[4] = "&nbsp;";
		grid01Array[5] = "&nbsp;";
		bgColor = "#F2F2F2";
		mkTable2("Grid01", grid01Array, bgColor, ",,,,,,", "", boldLine);
}


//## 휴일근무 상세정보 표시 ####################
function fnSetOffWork(seq)
{
	var valUrl = "/action/offDayWork/view.asp";
	var param = "seq="+seq;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();			
			$("#workDt").val($(xml).find("workDt").text());
			$("#workTm").val($(xml).find("workTm").text());
			$("#userId").val($(xml).find("userId").text());
			$("#seq").val($(xml).find("seq").text());
			$("#flag").val($(xml).find("flag").text());

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 휴일근무  정보 Reset 기능
function fnReset()
{
	$("#userId").val("");
	$("#workDt").val("");
	$("#workTm").val("");
	$("#seq").val("");
	$("#flag").val("Y");
}


//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findUserId").val("");
}